﻿Imports log4net

Partial Class P04_Edit
    Inherits System.Web.UI.Page

    Public scripttext As String
    Private Shared logger As ILog = LogManager.GetLogger("P04_Edit")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            scripttext = "<script>ClosePop();</script>"
        End If
        Session.LCID = 2057
        year1.Text = Val(Request("PlanYear") - 1).ToString
        year2.Text = Request("PlanYear")
        Dim re_adap As New FormLockDSTableAdapters.FundCenterReleasesTableAdapter
        If re_adap.CountRelease(Val(Request("PlanYear")), Request("Version"), Request("FCTR")) > 0 Then
            SaveDocument.Visible = False
        End If
        If IsPostBack = False Then
            LoadData()
            scripttext = "<script>CalSum();</script>"
        End If

    End Sub

    Sub LoadData()
        Dim adapter As New PDataSetTableAdapters.BudgetRevPlans2TableAdapter
        Dim datatable As New PDataSet.BudgetRevPlans2DataTable

        Dim sql As String
        sql = "with temp as(  select BudgetRevPlans.id,BudgetRevPlans.FundCenter_FundCenterCode,FundCenters.Description As FCName,BudgetRevplans.Product_ProductCode AS ProductCode,Products.Description As ProductDesc,BudgetRevPlans.ProductItem_Id,ProductItems.Description As ProItemDesc"
        sql += " ,ActualLastYear2,ActualLastYear1,ForecastLastYear1,ForecastCurYear,LastYearMonth6,LastYearMonth7,LastYearMonth8,LastYearMonth9,LastYearMonth10,LastYearMonth11,LastYearMonth12,CurYearMonth1,CurYearMonth2,CurYearMonth3,CurYearMonth4,CurYearMonth5,CurYearMonth6,CurYearMonth7,CurYearMonth8,CurYearMonth9,CurYearMonth10,CurYearMonth11,CurYearMonth12,(ActualLastYear1+ForecastLastYear1) As SumFX"
        ',LastYearMonth1,LastYearMonth2,LastYearMonth3,LastYearMonth4,LastYearMonth5
        sql += " , ProductItems.ItemNo"
        sql += " From budgetRevPlans INNER JOIN FundCenters ON BudgetRevPlans.FundCenter_FundCenterCode = FundCenters.FundCenterCode"
        sql += " INNER JOIN Products ON BudgetRevPlans.Product_ProductCode = Products.ProductCode INNER JOIN ProductItems ON BudgetRevplans.ProductItem_Id = ProductItems.Id"

        sql += " AND BudgetRevPlans.FundCenter_FundCenterCode = '" & Request("FCTR") & "'"
        sql += " AND BudgetRevPlans.Product_ProductCode = '" & Request("Product") & "'"
        sql += " And ProductItems.FormID = 'P04' "

        sql += " AND BudgetRevPlans.PlanYear = " & Request("PlanYear") & " "
        sql += " AND BudgetRevPlans.Version = '" & Request("Version") & "'"
        sql += " AND FundCenters.PlanYear = " & Request("PlanYear") & " "
        sql += " AND Products.PlanYear = " & Request("PlanYear") & " "
        sql += " AND ProductItems.PlanYear = " & Request("PlanYear") & " "

        'temp is main
        sql += " )"
        sql += " select temp.*"
        sql += " ,isnull(LastyearAct.LastYearMonth1,0) as LastYearMonth1"
        sql += " ,isnull(LastyearAct.LastYearMonth2,0) as LastYearMonth2 "
        sql += " ,isnull(LastyearAct.LastYearMonth3,0) as LastYearMonth3"
        sql += " ,isnull(LastyearAct.LastYearMonth4,0) as LastYearMonth4 "
        sql += " ,isnull(LastyearAct.LastYearMonth5,0) as LastYearMonth5  "
        sql += " from temp left join "

        sql += "  (select BudgetDetails.FundCenter_FundCenterCode as "
        sql += " fcode2,BudgetDetails.Product_ProductCode as p2"
        sql += " ,ABS ( sum(BudgetDetails.Actual1)) as LastYearMonth1 "
        sql += " ,ABS ( sum(BudgetDetails.Actual2))as LastYearMonth2 "
        sql += " ,ABS ( sum(BudgetDetails.Actual3))as LastYearMonth3 "
        sql += " ,ABS ( sum(BudgetDetails.Actual4))as LastYearMonth4 "
        sql += " ,ABS ( sum(BudgetDetails.Actual5))as LastYearMonth5 "

        sql += "  ,(select ProductItem_Id "
        sql += "   from ProductItemRevs"
        sql += " where PlanYear =" & year1.Text
        sql += " and FormId='P04'"
        sql += " and Product_ProductCode=BudgetDetails.Product_ProductCode "
        sql += " and Cmmt_CmmtCode=BudgetDetails.Cmmt_CmmtCode )as ProductItem_Id"

        sql += " from BudgetDetails"
        sql += " where Version='A01' and BudgetDetails.PlanYear=" & year1.Text
        sql += " and BudgetDetails.Product_ProductCode in  "
        sql += " (select p.Product_ProductCode from ProductItemRevs as"
        sql += " p where p.Cmmt_CmmtCode=BudgetDetails.Cmmt_CmmtCode "
        sql += " and p.Product_ProductCode=BudgetDetails.Product_ProductCode "
        sql += " and p.PlanYear=" & year1.Text & " and p.formId='P04') "
        sql += " group by BudgetDetails.FundCenter_FundCenterCode,BudgetDetails.Product_ProductCode,BudgetDetails.Cmmt_CmmtCode"
        sql += " )"
        sql += " as LastyearAct "
        sql += " on temp.FundCenter_FundCenterCode = LastyearAct.fcode2"
        sql += " and temp.ProductCode=LastyearAct.p2 "
        'sql += " and temp.ProductItem_Id=LastyearAct.ProductItem_Id"
        sql += " ORDER BY temp.FundCenter_FundCenterCode, temp.Id"
        'Response.Write(sql)
        adapter.GetDataByFilter(datatable, sql)
        If datatable.Count > 0 Then
            name1.Text = datatable.Item(0).FundCenter_FundCenterCode & " - " & datatable.Item(0).ProductDesc
        End If

        For Each item As PDataSet.BudgetRevPlans2Row In datatable
            A_ID.Text = item.id
            A_LAST2.Text = item.ActualLastYear2
            A1.Value = Format(item.LastYearMonth1 / 1000, "#,##0.##")
            A2.Value = Format(item.LastYearMonth2 / 1000, "#,##0.##")
            A3.Value = Format(item.LastYearMonth3 / 1000, "#,##0.##")
            A4.Value = Format(item.LastYearMonth4 / 1000, "#,##0.##")
            A5.Value = Format(item.LastYearMonth5 / 1000, "#,##0.##")
            ASUM0.Value = Format(item.ActualLastYear1 / 1000, "#,##0.##")

            A6.Value = Format(item.LastYearMonth6 / 1000, "#,##0.##")
            A7.Value = Format(item.LastYearMonth7 / 1000, "#,##0.##")
            A8.Value = Format(item.LastYearMonth8 / 1000, "#,##0.##")
            A9.Value = Format(item.LastYearMonth9 / 1000, "#,##0.##")
            A10.Value = Format(item.LastYearMonth10 / 1000, "#,##0.##")
            A11.Value = Format(item.LastYearMonth11 / 1000, "#,##0.##")
            A12.Value = Format(item.LastYearMonth12 / 1000, "#,##0.##")
            ASUM1.Value = Format(item.ForecastLastYear1 / 1000, "#,##0.##")

            A13.Value = Format(item.CurYearMonth1 / 1000, "#,##0.##")
            A14.Value = Format(item.CurYearMonth2 / 1000, "#,##0.##")
            A15.Value = Format(item.CurYearMonth3 / 1000, "#,##0.##")
            A16.Value = Format(item.CurYearMonth4 / 1000, "#,##0.##")
            A17.Value = Format(item.CurYearMonth5 / 1000, "#,##0.##")
            A18.Value = Format(item.CurYearMonth6 / 1000, "#,##0.##")
            A19.Value = Format(item.CurYearMonth7 / 1000, "#,##0.##")
            A20.Value = Format(item.CurYearMonth8 / 1000, "#,##0.##")
            A21.Value = Format(item.CurYearMonth9 / 1000, "#,##0.##")
            A22.Value = Format(item.CurYearMonth10 / 1000, "#,##0.##")
            A23.Value = Format(item.CurYearMonth11 / 1000, "#,##0.##")
            A24.Value = Format(item.CurYearMonth12 / 1000, "#,##0.##")
            ASUM2.Value = Format(item.ForecastCurYear / 1000, "#,##0.##")

        Next
    End Sub

    Protected Sub SaveDocument_Click(sender As Object, e As EventArgs) Handles SaveDocument.Click
        Dim adapterX As New PDataSetTableAdapters.BudgetRevPlans1TableAdapter
        Try
            adapterX.UpdateQuery(Val(A_LAST2.Text), Val(ASUM0.Value.Replace(",", String.Empty)) * 1000, Val(ASUM1.Value.Replace(",", String.Empty)) * 1000, Val(ASUM2.Value.Replace(",", String.Empty)) * 1000, Val(A1.Value.Replace(",", String.Empty)) * 1000, Val(A2.Value.Replace(",", String.Empty)) * 1000, Val(A3.Value.Replace(",", String.Empty)) * 1000, Val(A4.Value.Replace(",", String.Empty)) * 1000, Val(A5.Value.Replace(",", String.Empty)) * 1000, Val(A6.Value.Replace(",", String.Empty)) * 1000, Val(A7.Value.Replace(",", String.Empty)) * 1000, Val(A8.Value.Replace(",", String.Empty)) * 1000, Val(A9.Value.Replace(",", String.Empty)) * 1000, Val(A10.Value.Replace(",", String.Empty)) * 1000, Val(A11.Value.Replace(",", String.Empty)) * 1000, Val(A12.Value.Replace(",", String.Empty)) * 1000, Val(A13.Value.Replace(",", String.Empty)) * 1000, Val(A14.Value.Replace(",", String.Empty)) * 1000, Val(A15.Value.Replace(",", String.Empty)) * 1000, Val(A16.Value.Replace(",", String.Empty)) * 1000, Val(A17.Value.Replace(",", String.Empty)) * 1000, Val(A18.Value.Replace(",", String.Empty)) * 1000, Val(A19.Value.Replace(",", String.Empty)) * 1000, Val(A20.Value.Replace(",", String.Empty)) * 1000, Val(A21.Value.Replace(",", String.Empty)) * 1000, Val(A22.Value.Replace(",", String.Empty)) * 1000, Val(A23.Value.Replace(",", String.Empty)) * 1000, Val(A24.Value.Replace(",", String.Empty)) * 1000, Date.Now(), Val(Session("users")), Val(A_ID.Text))
            logger.Info("User " & Session("users") & " update product " & name1.Text)

            scripttext = "<script>ClosePop();</script>"
        Catch ex As Exception
            logger.Error(ex.Message)
            logger.Error(ex.StackTrace)

            scripttext = "<script>alert('ไม่สามารถบันทึกได้เนื่องจากจำนวนเงินเกินค่าที่ระบบจะรับได้');</script>"
        End Try
    End Sub
End Class
